const { Controller } = require("egg");
const { getResBody, getReqParams, responseData } = require("../../utils");
const { getTablesSelectSql } = require("../../scripts/note/core/creator/mysql/_utils/tablesSelect");

const createRule = {
  username: {
    type: "email",
  },
  password: {
    type: "password",
    compare: "re-password",
  },
};

module.exports = class TestController extends Controller {
  index(ctx) {
    // ctx.validate(createRule);
    // ctx.body = "测试页面";
    // ctx.body = "hi, egg1111";
    const { app } = this;
    const { type = "standTables" } = getReqParams(ctx);
    // 可选值：sysTables, sqlFields formItems tableCols insertRows
    const data = app[type];
    responseData(ctx, { data }); // 直接响应数据
  }
  search(ctx) {
    const type = ctx.query.type;
    const q = ctx.query.q || "nodejs";
    if (type === "bing") {
      ctx.redirect(`http://cn.bing.com/search?q=${q}`); // 必应搜索
    } else {
      ctx.redirect(`https://www.google.co.kr/search?q=${q}`); // 谷歌搜索
    }
  }
  async listApp() {
    // GET /projects/1/app/2
    assert.equal(this.ctx.params.projectId, "1");
    assert.equal(this.ctx.params.appId, "2");

    // POST /api/posts HTTP/1.1
    // {"title": "controller", "content": "what is controller"}
    // assert.equal(this.ctx.request.body.title, 'controller');
    // assert.equal(this.ctx.request.body.content, 'what is controller');
  }
  // 处理校验异常
  async handleValidate() {
    const ctx = this.ctx;
    try {
      ctx.validate(createRule);
    } catch (err) {
      ctx.logger.warn(err.errors);
      ctx.body = { success: false };
      return;
    }
  }
  async sqlTest() {
    const { ctx } = this;
    getTablesSelectSql();
    ctx.body = getTablesSelectSql();
  }
};
